package com.magicborrow.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.magicborrow.R;

import java.util.ArrayList;

/**
 * Created by River on 5/15/16.
 */
public class SearchHistoryAdapter extends BaseAdapter{

    private ArrayList<String> data;
    private Context context;

    public SearchHistoryAdapter(Context context){
        this.context = context;
    }

    public void setData(ArrayList<String> data){
        this.data = data;
    }

    public ArrayList<String> getData(){
        return this.data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return getItemId(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoder holder;
        //观察convertView随ListView滚动情况
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.search_history_item,null);
            holder = new ViewHoder();
            /**得到各个控件的对象*/
            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);//绑定ViewHolder对象
        }
        else{
            holder = (ViewHoder)convertView.getTag();//取出ViewHolder对象
        }
//        convertView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context,"sdf",Toast.LENGTH_SHORT).show();
//            }
//        });
        /**设置TextView显示的内容，即我们存放在动态数组中的数据*/
        holder.name.setText(data.get(position));
        return convertView;
    }

    public class ViewHoder{
        TextView name;
    }

}
